(function(window,$){
	'use strict';
	function ImgRun(id,option){
		var defaultOption = {
			list:[],	/*{src:,href:}*/
			width:null,
			height:null,
			point:true,
			pointColor:"#ffffff"
		}
		var e = this;
		e.o = $.extend(defaultOption,option);
		e.id = id;
		e.cp = 0;
		e.l = e.o.list.length;
		e.init();
	}
	
	ImgRun.prototype.init = function(){
		var e = this;
		e.createImg();
		e.run();
		if(e.o.point){
			e.createPoint();
		}
	}
	
	ImgRun.prototype.createImg = function(){
		var e = this;
		e.o.list.forEach(function(item,index){
			var cp = index == 0 ? 100 : 99;
			$(e.id).css('position','relative').append(
				$("<a class='img_item'></a>").attr('href',item.href||"#").css({
					backgroundImage:"url(" + item.src + ")",
					zIndex:cp,
					position: "absolute",
					display: "inline-block",
					width: "100%",
					height: "100%",
					backgroundSize: "cover",
					backgroundPosition: "center",
					backgroundRepeat: "no-repeat",
					top: 0,
				})
			);
		})
	}
	ImgRun.prototype.run = function(){
		var e = this;
		clearInterval(e.timer);
		e.timer = setInterval(function(){
			e.cp++;
			e.cp = e.cp < e.l ? e.cp : 0;
			$(e.id).children('.img_item').eq(e.cp).css('z-index',100).siblings('.img_item').css('z-index',99);
			$(e.id).children('.point_box').children('.point')
				.eq(e.cp).css('background-color','#FFFFFF').siblings().css('background-color','gray');
		},2000);
	}
	ImgRun.prototype.createPoint = function(){
		var e = this;
		var box = $("<p class='point_box'></p>").css({
			position: "absolute",
			bottom: 0,
			zIndex: 110,
			margin: 0,
			padding: 0,
			width: "100%",
			height: "30px",
			padding: "10px 0",
			textAlign: "center",
		});
		var bgColor;
		e.o.list.forEach(function(item,index){
			bgColor = index == 0 ? "#ffffff" : "gray";
			box.append($("<span class='point'></span>").css({
				backgroundColor:bgColor,
				display: "inline-block",
				borderRadius: "50%",
				width: "10px",
				height: "10px",
				margin: "0 10px",
			}).click(function(){
				var i = $(this).index();
				if(i == e.cp){return};
				e.cp = i;
				clearInterval(e.timer);
				setTimeout(function(){
					e.run();
				},3000)
				$(this).css("background-color",'#FFFFFF').siblings('.point').css('background-color','gray');
				$(e.id).children('.img_item').eq(i).css('z-index',100).siblings('.img_item').css('z-index',99);
			}))
		})
		$(e.id).append(box);
	}
	
	return window.ImgRun = ImgRun;
	
})(window,jQuery)
